package com.gpframework.module.controller.ctn;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.module.ctn.domain.*;
import com.gpframework.module.ctn.domain.req.GpReceiptGoodsAdd;
import com.gpframework.module.ctn.domain.req.GpReceiptGoodsReq;
import com.gpframework.module.ctn.domain.req.GpReceiptGoodsUpdate;
import com.gpframework.module.ctn.domain.vo.GpReceiptGoodsVo;
import com.gpframework.module.ctn.service.*;
import com.gpframework.module.module.domain.GpAgentCompany;
import com.gpframework.module.module.domain.GpCustomerManagement;
import com.gpframework.module.module.domain.GpSupplierManagement;
import com.gpframework.module.module.service.IGpAgentCompanyService;
import com.gpframework.module.module.service.IGpCustomerManagementService;
import com.gpframework.module.module.service.IGpSupplierManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpReceiptGoodsController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 录入订单Controller 接口类
 */
@Validated
@Api(tags = "录入订单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/ctn/receiptgoods" )
public class GpReceiptGoodsController extends BaseController {

    private final IGpReceiptGoodsService iGpReceiptGoodsService;
    private final IGpPackagesInformationService iGpPackagesInformationService;
    private final IGpCtnLoadedService iGpCtnLoadedService;
    private final IGpNigeriaWarehouseReleaseService iGpNigeriaWarehouseReleaseService;
    private final IGpHoldGoodsChinaReleaseService iGpHoldGoodsChinaReleaseService;
    private final IGpNigeriaMarketCheckService iGpNigeriaMarketCheckService;
    private final IGpCustomerManagementService iGpCustomerManagementService;
    private final IGpSupplierManagementService iGpSupplierManagementService;
    private final IGpAgentCompanyService iGpAgentCompanyService;

    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpReceiptGoods
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo<GpReceiptGoodsVo> list(GpReceiptGoodsReq gpReceiptGoodsReq) {
        startPage();
        QueryWrapper<GpReceiptGoods> lqw = new QueryWrapper<GpReceiptGoods>();
        gpReceiptGoodsReq.generatorQuery(lqw, true);
        List<GpReceiptGoodsVo> list = iGpReceiptGoodsService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpReceiptGoods
     * @Return AjaxResult
     */
    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public AjaxResult<GpReceiptGoodsVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iGpReceiptGoodsService.getVoById(id));
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpReceiptGoods
     * @Return AjaxResult
     */
    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpReceiptGoodsAdd gpReceiptGoodsAdd) {
        GpReceiptGoods receiptNo = iGpReceiptGoodsService.getOne(new QueryWrapper<GpReceiptGoods>().eq("receipt_no", gpReceiptGoodsAdd.getReceiptNo()).eq("del_status", YesNoEnum.NO.getKey()));
        if (receiptNo != null) {
            return AjaxResult.error("进仓单号重复");
        }
        GpReceiptGoods gpReceiptGoods = Convert.convert(new TypeReference<GpReceiptGoods>() {
        }, gpReceiptGoodsAdd);
        GpCustomerManagement customerManagement = iGpCustomerManagementService.getById(gpReceiptGoodsAdd.getCustomerId());
        GpSupplierManagement gpSupplierManagement = iGpSupplierManagementService.getById(gpReceiptGoodsAdd.getSupplierId());
        GpAgentCompany agentCompany = iGpAgentCompanyService.getById(gpReceiptGoodsAdd.getAgentCompanyId());
        gpReceiptGoods.setCustomer(customerManagement.getCustomer());
        gpReceiptGoods.setCustomerTel(customerManagement.getCustomerTel());
        gpReceiptGoods.setSupplier(gpSupplierManagement.getSupplier());
        gpReceiptGoods.setSupplierTel(gpSupplierManagement.getSupplierTel());
        gpReceiptGoods.setAgentCompany(agentCompany.getAgentCompany());
        gpReceiptGoods.setAgentCompanyTel(agentCompany.getAgentCompanyTel());
        return toAjax(iGpReceiptGoodsService.save(gpReceiptGoods) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpReceiptGoods
     * @Return AjaxResult
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpReceiptGoodsUpdate gpReceiptGoodsUpdate) {
        GpReceiptGoods receiptNo = iGpReceiptGoodsService.getOne(new QueryWrapper<GpReceiptGoods>().eq("receipt_no", gpReceiptGoodsUpdate.getReceiptNo()).eq("del_status", YesNoEnum.NO.getKey()).ne("id", gpReceiptGoodsUpdate.getId()));
        if (receiptNo != null) {
            return AjaxResult.error("进仓单号重复");
        }
        GpReceiptGoods gpReceiptGoods = Convert.convert(new TypeReference<GpReceiptGoods>() {
        }, gpReceiptGoodsUpdate);
        return toAjax(iGpReceiptGoodsService.updateById(gpReceiptGoods) ? 1 : 0);
    }

    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpReceiptGoods
     * @Return AjaxResult
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            iGpPackagesInformationService.update(new UpdateWrapper<GpPackagesInformation>().set("del_status", YesNoEnum.YES.getKey()).eq("receipt_id", id));
            iGpCtnLoadedService.update(new UpdateWrapper<GpCtnLoaded>().set("del_status", YesNoEnum.YES.getKey()).eq("receipt_id", id));
            iGpNigeriaMarketCheckService.update(new UpdateWrapper<GpNigeriaMarketCheck>().set("del_status", YesNoEnum.YES.getKey()).eq("receipt_id", id));
            iGpHoldGoodsChinaReleaseService.update(new UpdateWrapper<GpHoldGoodsChinaRelease>().set("del_status", YesNoEnum.YES.getKey()).eq("receipt_id", id));
            iGpNigeriaWarehouseReleaseService.update(new UpdateWrapper<GpNigeriaWarehouseRelease>().set("del_status", YesNoEnum.YES.getKey()).eq("receipt_id", id));
        }
        return toAjax(iGpReceiptGoodsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
